﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 控制摄像机类（用四层结构控制）
/// 1.初始化摄像机位置
/// 2.设置摄像机的跟随旋转、上下移动和放缩
/// 3.使摄像机实时对着主角
/// </summary>
public class CameraCtrl : MonoBehaviour {

    public static CameraCtrl Instance;          //单例

    [SerializeField]
    private Transform m_CameraUpAndDown;        //摄像机上下
    [SerializeField]
    private Transform m_CameraZoomContainer;    //摄像机放缩
    [SerializeField]
    private Transform m_CameraContainer;        //摄像机容器

    void Awake()
    {
        Instance = this;                        //单例赋值
    }

    //初始化摄像机镜头
    public void Init()
    {
        m_CameraUpAndDown.transform.localEulerAngles = new Vector3(0, 0, Mathf.Clamp(m_CameraUpAndDown.transform.localEulerAngles.z, 35f, 80f));
    }

    //设置摄像机旋转（0左，1右）
    public void SetCameraRotate(int type)
    {
        this.transform.Rotate(0, 80 * Time.deltaTime * (type == 0 ? -1 : 1), 0);

    }

    //设置摄像机上下（0上，1下）
    public void SetCameraUpAndDown(int type)
    {
        m_CameraUpAndDown.transform.Rotate(0, 0, 60 * Time.deltaTime * (type == 1 ? -1 : 1));
        m_CameraUpAndDown.transform.localEulerAngles = new Vector3(0, 0, Mathf.Clamp(m_CameraUpAndDown.transform.localEulerAngles.z, 35f, 80f));
    }

    //设置摄像机放缩（0近，1远）
    public void SetCameraZoom(int type)
    {
        m_CameraContainer.Translate(Vector3.forward * 10 * Time.deltaTime * ((type == 1 ? -1 : 1)));
        m_CameraContainer.localPosition = new Vector3(Mathf.Clamp(m_CameraContainer.localPosition.x, -3f, 3f), 0, 0);
    }

    //实时对着主角
    public void AutoLookAt(Vector3 pos)
    {
        m_CameraContainer.LookAt(pos);
    }

    //辅助
    void OnDrawGizmos()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(this.transform.position, 15f);
    }
}
